package com.loozb.thread4;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 龙召碧
 * @create 2018-02-01 16:10
 **/
public class ThreadLocalDemo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<SimpleDateFormat>();
    public static class ParseDate implements Runnable {
        int i = 0;
        public ParseDate(int i) {this.i = i;}
        public static synchronized void option(int i) {
            try {
                Date t = sdf.parse("2018-02-01 16:13:" + i%60);
                System.out.println(t);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            try {
                if(t1.get() == null) {
                    t1.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                }
                Date t = t1.get().parse("2018-02-01 16:37:" + i%60);
                System.out.println(i + ":" + t);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(10);
        for(int i = 0; i < 1000; i++) {
            es.execute(new ParseDate(i));
        }
        t1.remove();
    }
}

